import {combination} from "../../utils/util";

class SkuCode {
    code;
    spuId;
    totalSegments = [];

    constructor(code) {
        this.code = code;
        this._splitToSegments();
    }

    _splitToSegments() {
        // 2$1-45#3-9#4-14
        //以$进行分割
        const spuAndSpec = this.code.split("$");
        this.spuId = spuAndSpec[0];

        //以#进行分割
        const specCodeArray = spuAndSpec[1].split("#");
        const length = specCodeArray.length;
        for (let i = 1; i <= length; i++) {
            //使用排列组合算法，来获得存在的待选规格
            const segments = combination(specCodeArray, i);
            //如果一个二维数组里有多个待选规格，把一维数组使用#进行拼接
            const newSegments = segments.map(segs => {
                return segs.join("#");
            });
            this.totalSegments = this.totalSegments.concat(newSegments);
            console.log(newSegments);
        }
    }

}

export {
    SkuCode
}